<?php
    global $db;

    // 8/24/2009: Immanuel192; get module config
    $config=$db->selectObject("albummodule_config","location_data = '".serialize($loc)."'");
    if (!$config) {
        $config->item_per_page = 10;
        $config->album_type =0;
    }
    $max_item=$config->item_per_page;
    // get module title
    // because if the client switch page, so, that is the action and exponent does not transfer $title and $views		
    $temp=$db->selectObject("container","internal = '" . serialize($loc) . "'");
    if ($temp)
    {
        $title=$temp->title;
        $view=$temp->view;
    }
    else
    {
        $title="";
        $view="Default";
    }

    // get all album here
    $count_post = $db->countObjects('album',"location_data='".serialize($loc)."'");

    // page split
    // cal how many posts
    $page_count=floor($count_post / $max_item);
    if ($count_post % $max_item != 0)
        $page_count++;

    // get page
    $page=1;
    if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
    if (($page<1) || ($page>$page_count)) $page=1;
    $start_pos=($page-1)*$max_item;

    $template = new template('albummodule',$view,$loc);

    if (!defined('SYS_FILES')) require_once(BASE.'subsystems/files.php');

    $albums= $db->selectObjects('album',"album_type={$config->album_type} AND location_data='".serialize($loc)."'ORDER BY rank DESC, id DESC LIMIT {$start_pos},{$max_item}");
    for($i=0; $i<count($albums); $i++) {
        // for seo support
        $albums[$i]->seo=_to_vietnamese_with_nomark($albums[$i]->name);
        if ($albums[$i]->image_id == 0) {
            $albums[$i]->picpath = '';
        } else {
            $file = $db->selectObject('file', 'id='.$albums[$i]->image_id);
            $albums[$i]->picpath = $file->directory.'/'.$file->filename;
        }

        // for rating
        $rate_1 = floor($albums[$i]->rate);
        $rate_half = $albums[$i]->rate - $rate_1;
        $rate_2 = 5- $rate_1 - (($rate_half > 0)?1:0);
        $rate=array();
        for ($j=1;$j<=$rate_1;$j++)
            $rate[$j]=1; // for active rate
        if ($rate_half > 0)
            $rate[$rate_1+1]=3; // for rate half
        for ($j=5-$rate_2+1;$j<=5;$j++)
            $rate[$j]=2; // for active rate
        $albums[$i]->rate=$rate;
        // end of rating

        // for format_type
        $temp=$db->selectObject("album_format","id = ".$albums[$i]->format);
        $albums[$i]->format_text = $temp->name;
        // end of format type
        // for genre 
        $temp=$db->selectObject("album_genre","id = ".$albums[$i]->genre);
        $albums[$i]->genre_text = $temp->name;
        // end of genre

        // count all music in this album
        $albums[$i]->musics=$db->countObjects('musics','album_id='.$albums[$i]->id);

        /* 8/18/2009: Immanuel192 commented this code
        // for all musics in this album
        $musics=$db->selectObjects('musics','album_id='.$albums[$i]->id, " id ASC");


        for($j=0; $j<count($musics); $j++) {
        $musics[$j]->downloadable=$albums[$i]->downloadable;
        $musics[$j]->singer_search = htmlspecialchars($musics[$j]->singer);

        if ($musics[$j]->high_quality == 0) {
        $musics[$j]->high_quality = '';
        } else {
        $file = $db->selectObject('file', 'id='.$musics[$j]->high_quality);
        $musics[$j]->high_quality = $file->directory.'/'.$file->filename;
        if (file_exists($musics[$j]->high_quality))
        {
        $file_info = $getid3->analyze($musics[$j]->high_quality); 				  
        //getid3_lib::CopyTagsToComments($file_info); 
        $musics[$j]->bitrate=round($file_info['bitrate']/1000,0);
        $musics[$j]->fileformat=$file_info['fileformat'];
        $file_info=$file_info['playtime_string'];
        $musics[$j]->file_length = $file_info;

        }
        }

        if ($musics[$j]->sound_trackmix == 0) {
        $musics[$j]->sound_trackmix = '';
        } else {
        $file = $db->selectObject('file', 'id='.$musics[$j]->sound_trackmix);
        $musics[$j]->sound_trackmix = $file->directory.'/'.$file->filename;
        }

        if ($musics[$j]->sheet_music == 0) {
        $musics[$j]->sheet_music = '';
        } else {
        $file = $db->selectObject('file', 'id='.$musics[$j]->sheet_music);
        $musics[$j]->sheet_music = $file->directory.'/'.$file->filename;
        }

        if ($musics[$j]->demo == 0) {
        $musics[$j]->demo = '';
        } else {
        $file = $db->selectObject('file', 'id='.$musics[$j]->demo);
        $musics[$j]->demo = $file->directory.'/'.$file->filename;
        }

        // get composer name
        $composer=$db->selectObject('listing',"id = {$musics[$j]->composer_id}");
        //eDebug($composer);
        $composer_name='';
        if ($composer)
        {
        $composer_name=$composer->name;
        }

        // for tooltip
        $musics[$j]=musicsmodule::make_info($musics[$j], $composer_name);


        }
        $albums[$i]->musics=$musics;*/
    }


    $template->register_permissions(array('administrate','configure'),$loc);
    $template->assign('albums', $albums);
    /*
    * Fix only for Compact view - trungdt2

    $file_name=@create_playlist($albums[0]->musics);
    $template->assign('plist',$file_name);*/
    $template->assign('moduletitle', $title);
    $template->assign('pages',$page_count);
    $template->assign('page',$page);
    if (!(isset($_REQUEST['flow_has_set']) && ($_REQUEST['flow_has_set']==1)))
        exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
    $template->output();

?>
